home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / sveditaeutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  8.3 KB  |  355 lines

  1. /*
  2.     File:        SVEditAEUtils.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                             3.1 updates by Greg Sutton.    
  8.  
  9.     Copyright:    Copyright ©1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #pragma segment Main
  25.  
  26. #include "SVEditAEUtils.h"
  27.  
  28. #include "SVEditUtils.h"
  29. #include "SVEditWindow.h"
  30.  
  31. #include "SVAERecording.h"
  32.  
  33. #include <AERegistry.h>
  34. #include <AEObjects.h>
  35.  
  36.  
  37. // -----------------------------------------------------------------------
  38. //    Utility Routines for getting data from AEDesc's
  39. // -----------------------------------------------------------------------
  40.     
  41. void    GetRawDataFromDescriptor(const AEDesc *theDesc,
  42.                                                                          Ptr     destPtr,
  43.                                                                          Size    destMaxSize,
  44.                                                                          Size    *actSize)
  45.   {
  46.       Size copySize;
  47.  
  48.  
  49.         if (theDesc->dataHandle) 
  50.             {
  51.                 HLock((Handle)theDesc->dataHandle);
  52.                 *actSize = GetHandleSize((Handle)theDesc->dataHandle);
  53.                 
  54.                 copySize = LesserOf(*actSize, destMaxSize);
  55.                 
  56.                 BlockMove(*theDesc->dataHandle, destPtr, copySize);
  57.                 
  58.                 HUnlock((Handle)theDesc->dataHandle);
  59.             }
  60.         else
  61.             *actSize = 0;
  62.     } /*GetRawDataFromDescriptor*/
  63.  
  64.  
  65. OSErr    GetPStringFromDescriptor(const AEDesc *aDesc, StringPtr resultStr)
  66. {
  67.     Size         stringSize;
  68.     AEDesc       resultDesc = {typeNull, NULL};
  69.     OSErr        err;
  70.     
  71.     err = AECoerceDesc(aDesc, typeChar, &resultDesc);
  72.     if (noErr != err) goto done;
  73.     
  74.     resultStr[0] = 0;
  75.     
  76.     GetRawDataFromDescriptor(&resultDesc, (Ptr)&resultStr[1], 255, &stringSize);
  77.     if (stringSize <= 255) 
  78.         resultStr[0] = stringSize;
  79.     else
  80.         err = errAECoercionFail;
  81.  
  82. done:        
  83.     if (resultDesc.dataHandle) 
  84.         AEDisposeDesc(&resultDesc);
  85.         
  86.     return(err);
  87. }
  88.     
  89.  
  90. OSErr    PutPStringToDescriptor(AEDesc* aDesc, StringPtr pStr)
  91. {
  92.     OSErr        err;
  93.     
  94.     if (! aDesc || ! pStr)
  95.         return(errAETypeError);
  96.  
  97.     err = AECreateDesc(typeChar, (Ptr)&pStr[1], pStr[0], aDesc);
  98.         
  99.     return(err);
  100. }
  101.  
  102.  
  103. OSErr    GetIntegerFromDescriptor(const AEDesc *sourceDesc, short *result)
  104.   {
  105.       OSErr   myErr;
  106.         OSErr   ignoreErr;
  107.         Size    intSize;
  108.         AEDesc  resultDesc;
  109.         
  110.         *result = 0;
  111.         myErr  = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
  112.         
  113.         if (myErr==noErr) 
  114.             {
  115.                 GetRawDataFromDescriptor(&resultDesc,
  116.                                                                  (Ptr)result,
  117.                                                                  2,
  118.                                                                  &intSize);
  119.                 if (intSize>2) 
  120.                     myErr = errAECoercionFail;
  121.             }
  122.         
  123.         if (resultDesc.dataHandle) 
  124.             ignoreErr = AEDisposeDesc(&resultDesc);
  125.             
  126.         return(myErr);
  127.     }
  128.     
  129. OSErr    GetBooleanFromDescriptor(const AEDesc *sourceDesc,
  130.                                                                         Boolean *result)
  131.   {
  132.       OSErr  myErr;
  133.         OSErr  ignoreErr;
  134.         Size   boolSize;
  135.         AEDesc resultDesc;
  136.         
  137.         *result = false;
  138.         myErr = AECoerceDesc(sourceDesc,typeBoolean,&resultDesc);
  139.         
  140.         if (myErr==noErr) 
  141.             {
  142.                 GetRawDataFromDescriptor(&resultDesc,
  143.                                                                  (Ptr)result,
  144.                                                                  sizeof(Boolean),
  145.                                                                  &boolSize);
  146.                 if (boolSize>sizeof(Boolean)) 
  147.                     myErr = errAECoercionFail;
  148.             }
  149.         
  150.         if (resultDesc.dataHandle) 
  151.             ignoreErr = AEDisposeDesc(&resultDesc);
  152.             
  153.         return(myErr);
  154.     }
  155.  
  156. OSErr    GetLongIntFromDescriptor(const AEDesc *sourceDesc, 
  157.                                       long   *result)
  158.   {
  159.       OSErr   myErr;
  160.         OSErr   ignoreErr;
  161.         Size    intSize;
  162.         AEDesc  resultDesc;
  163.         
  164.         *result = 0;
  165.         myErr = AECoerceDesc(sourceDesc,typeLongInteger,&resultDesc);
  166.         
  167.         if (myErr==noErr) 
  168.             {
  169.                 GetRawDataFromDescriptor(&resultDesc,
  170.                                                                  (Ptr)result,
  171.                                                                  4,
  172.                                                                  &intSize);
  173.                 if (intSize>4) 
  174.                     myErr = errAECoercionFail;
  175.             }
  176.         
  177.         if (resultDesc.dataHandle) 
  178.             ignoreErr = AEDisposeDesc(&resultDesc);
  179.             
  180.         return(myErr);
  181.     } /*GetLongIntFromDescriptor*/
  182.  
  183. OSErr    GetRectFromDescriptor(const AEDesc *sourceDesc, Rect *result)
  184.     {
  185.         OSErr   myErr;
  186.         OSErr   ignoreErr;
  187.         Size    rectSize;
  188.         AEDesc  resultDesc;
  189.             
  190.         SetRect(result,0,0,0,0);
  191.         myErr = AECoerceDesc(sourceDesc,typeQDRectangle,&resultDesc);
  192.         
  193.         if (myErr==noErr) 
  194.             {
  195.                 GetRawDataFromDescriptor(&resultDesc,
  196.                                                                  (Ptr)result,
  197.                                                                  sizeof(Rect),
  198.                                                                  &rectSize);
  199.                 if (rectSize<sizeof(Rect)) 
  200.                     myErr = errAECoercionFail;
  201.             }
  202.         
  203.         if (resultDesc.dataHandle) 
  204.             ignoreErr = AEDisposeDesc(&resultDesc);
  205.             
  206.         return(myErr);
  207.     } /*GetRectFromDescriptor*/
  208.  
  209. OSErr    GetPointFromDescriptor(const AEDesc *sourceDesc,
  210.                                                                   Point  *result)
  211.   {
  212.       OSErr   myErr;
  213.         OSErr   ignoreErr;
  214.         Size    ptSize;
  215.         AEDesc  resultDesc;
  216.         
  217.         SetPt(result,0,0);
  218.         
  219.         myErr = AECoerceDesc(sourceDesc,typeQDPoint,&resultDesc);
  220.         
  221.         if (myErr==noErr) 
  222.             {
  223.                 GetRawDataFromDescriptor(&resultDesc,
  224.                                                                  (Ptr)result,
  225.                                                                  sizeof(Point),
  226.                                                                  &ptSize);
  227.                                                                  
  228.                 if (ptSize<sizeof(Point)) 
  229.                     myErr = errAECoercionFail;
  230.                     
  231.             }
  232.         
  233.         if (resultDesc.dataHandle) 
  234.             ignoreErr = AEDisposeDesc(&resultDesc);
  235.             
  236.         return(myErr);
  237.     } /*GetPointFromDescriptor*/
  238.  
  239.  
  240. // ----------------------------------------------------------------------
  241. //    Name:        GotRequiredParams
  242. //    Function:    Checks all parameters defined as 'required' have been read
  243. // ----------------------------------------------------------------------
  244.                             
  245. OSErr    GotRequiredParams(const AppleEvent *theAppleEvent)
  246. {
  247.     DescType    returnedType;
  248.     Size        actualSize;
  249.     OSErr        err;
  250.     
  251.         // look for the keyMissedKeywordAttr, just to see if it's there
  252.     
  253.     err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  254.                                                 &returnedType, NULL, 0, &actualSize);
  255.     
  256.     switch (err)
  257.     {
  258.         case errAEDescNotFound:        // attribute not there means we
  259.             err = noErr;            // got all required parameters.
  260.             break;
  261.             
  262.         case noErr:                    // attribute there means missed
  263.             err = errAEParamMissed;    // at least one parameter.
  264.             break;
  265.             
  266.         // default:        pass on unexpected error in looking for the attribute
  267.     }
  268.     
  269.     return(err);
  270. } // GotReqiredParams
  271.  
  272.  
  273. // This routine takes a result descriptor, a reply descriptor and an error.
  274. // If there is a result to add to the reply it makes sure the reply isn't
  275. // NULL itself then adds the result to the reply depending on the error
  276. // and the type of result.
  277.  
  278. OSErr    AddResultToReply(AEDesc* result, AEDesc* reply, OSErr error)
  279. {
  280.     OSErr    err;
  281.  
  282.         // Check that the reply is not NULL and there is a result to put in it  
  283.     if (typeNull == reply->descriptorType || typeNull == result->descriptorType)
  284.         return(error);
  285.     
  286.     if (noErr == error)
  287.         err = AEPutParamDesc(reply, keyDirectObject, result);
  288.     else
  289.     {
  290.         switch (result->descriptorType)
  291.         {
  292.             case typeInteger:
  293.                 err = AEPutParamDesc(reply, keyErrorNumber, result);
  294.                 break;
  295.                 
  296.             case typeChar:
  297.                 err = AEPutParamDesc(reply, keyErrorString, result);
  298.                 break;
  299.                 
  300.             default:
  301.                 err = errAETypeError;
  302.         }
  303.         
  304.         if (noErr == err)
  305.             err = error;        // Don't loose that error
  306.     }
  307.  
  308.     return(err);
  309. }
  310.  
  311. // -----------------------------------------------------------------------
  312. //        Name:             MakeSelfAddress
  313. //        Purpose:        Builds an AEAddressDesc for the current process
  314. // -----------------------------------------------------------------------
  315.     
  316. OSErr    MakeSelfAddress(AEAddressDesc *selfAddress)
  317. {
  318.     ProcessSerialNumber procSerNum;
  319.     
  320.     procSerNum.highLongOfPSN = 0;
  321.     procSerNum.lowLongOfPSN  = kCurrentProcess;
  322.     
  323.     return(AECreateDesc(typeProcessSerialNumber, (Ptr)&procSerNum, sizeof(procSerNum), selfAddress));
  324. } // MakeSelfAddress
  325.  
  326.  
  327.  
  328. OSErr    GetEnumeratedFromDescriptor(const AEDesc *sourceDesc, DescType  *result)
  329. {
  330.     OSErr   myErr;
  331.     OSErr   ignoreErr;
  332.     Size    enumSize;
  333.     AEDesc  resultDesc;
  334.     
  335.     *result = typeNull;
  336.     
  337.     myErr = AECoerceDesc(sourceDesc,typeEnumerated,&resultDesc);
  338.     
  339.     if (myErr==noErr)
  340.     {
  341.         GetRawDataFromDescriptor(&resultDesc, (Ptr)result, sizeof(DescType), &enumSize);
  342.     
  343.         if (enumSize<sizeof(DescType))
  344.             myErr = errAECoercionFail;
  345.     }
  346.     
  347.     if (resultDesc.dataHandle)
  348.         ignoreErr = AEDisposeDesc(&resultDesc);
  349.     
  350.     return(myErr);
  351. } // GetEnumeratedFromDescriptor
  352.  
  353.  
  354.  
  355.